package com.bangbang.privatechat;

import com.alibaba.fastjson.JSONObject;
import com.sun.deploy.util.SessionState;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class client {
    public static boolean connection_state = false;
    private static Socket socket;
    private static ObjectOutputStream oos;

    public static void main(String[] args) {
        connect();

    }

    private static void connect(){
        try {
            socket = new Socket("127.0.0.1",9999);
            connection_state = true;
            oos = new ObjectOutputStream(socket.getOutputStream());
            new Thread(new Client_listen(socket)).start();
            new Thread(new Client_send(socket,oos)).start();
            new Thread(new Client_heart(socket,oos)).start();
        } catch (IOException e) {
            e.printStackTrace();
            connection_state = false;
        }
    }

    public static void reconnect(){
        while (!connection_state){
            System.out.println("正在尝试重新链接.....");
            connect();
            try{
                Thread.sleep(3000);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
}

class Client_listen implements Runnable{
    private Socket socket;

    Client_listen(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            while (true){
                System.out.println(ois.readObject());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Client_send implements Runnable{
    private Socket socket;
    private ObjectOutputStream oos;

    Client_send(Socket socket,ObjectOutputStream oos){
        this.socket = socket;
        this.oos = oos;
    }
    @Override
    public void run() {
        try {
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入:");
                String string = scanner.nextLine();
                JSONObject object = new JSONObject();
                object.put("type","chat");
                object.put("msg",string);
                oos.writeObject(object);
                oos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }try{
            socket.close();
            client.connection_state = false;
            client.reconnect();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Client_heart implements Runnable{
    private Socket socket;
    private ObjectOutputStream oos;
    Client_heart(Socket socket,ObjectOutputStream oos){
        this.socket = socket;
        this.oos = oos;
    }
    @Override
    public void run() {
        try {
            System.out.println("心跳包线程已启动");
            while (true){
                Thread.sleep(5000);
                JSONObject object = new JSONObject();
                object.put("type","heart");
                object.put("msg","心跳包");
                oos.writeObject(object);
                oos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }try{
            socket.close();
            client.connection_state = false;
            client.reconnect();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
